package com.yunzheng.modoules.enrollAndLogin.enroll.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunzheng.modoules.enrollAndLogin.enroll.service.MemberService;
import com.yunzheng.modoules.enrollAndLogin.enroll.service.MemberServiceImpl;
import com.yunzheng.modoules.enrollAndLogin.login.service.UserService;
import com.yunzheng.modoules.enrollAndLogin.login.service.UserServiceImpl;
import com.yunzheng.modoules.response.ResponseWrapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "EnrollServlet",value = "/EnrollServlet")
public class EnrollServlet extends HttpServlet {

    UserService userService = new UserServiceImpl();

    MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        

        //获取请求体数据
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        //将JSON字符转化为JAVA对象
        JSONObject jsonObject = JSON.parseObject(readLine);
        String username = jsonObject.getString("username");
        String gender = jsonObject.getString("gender");
        String direction = jsonObject.getString("direction");
        String term = jsonObject.getString("term");
        String invite_code = jsonObject.getString("invite_code");
        String phone_num = jsonObject.getString("phone_num");
        String password = jsonObject.getString("password");


        if (invite_code.equals("Welcome_To_YZ")){
            try {
                if (memberService.isExistMember(username,term,direction)){
                    if (memberService.isEnroll(username,term,direction,gender,phone_num,password)){
                        ResponseWrapper responseWrapper = ResponseWrapper.SuccessButNoData();
                        // request.setAttribute("requestWrapper",responseWrapper);
                        String s = JSON.toJSONString(responseWrapper);
                        PrintWriter writer = response.getWriter();
                        writer.println(s);
                        System.out.println(s);
                        //response.sendRedirect("Login.jsp");
                    }else if (userService.isExist(phone_num)){
                        ResponseWrapper responseWrapper = ResponseWrapper.EXIST_FAILURE();
                        // request.setAttribute("requestWrapper",responseWrapper);
                        String s = JSON.toJSONString(responseWrapper);
                        PrintWriter writer = response.getWriter();
                        writer.println(s);
                        System.out.println(s);
                        //request.getRequestDispatcher("enroll.jsp").forward(request,response);
                    }
                }else {
                    ResponseWrapper responseWrapper = ResponseWrapper.MEMBER_FAILURE();
                    // request.setAttribute("requestWrapper",responseWrapper);
                    String s = JSON.toJSONString(responseWrapper);
                    PrintWriter writer = response.getWriter();
                    writer.println(s);
                    System.out.println(s);
                    //request.getRequestDispatcher("enroll.jsp").forward(request,response);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else {
            ResponseWrapper responseWrapper = ResponseWrapper.INVITE_FAILURE();
            // request.setAttribute("requestWrapper",responseWrapper);
            String s = JSON.toJSONString(responseWrapper);
            PrintWriter writer = response.getWriter();
            writer.println(s);
            System.out.println(s);
            //request.getRequestDispatcher("enroll.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
